package com.crazy.util;

import com.crazy.enums.DateFormatEnum;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author panyonghui
 * @describe 日期工具类
 * @date 2022/1/14 17:40
 */
public class DateUtils {
    /**
     * yyyyMMdd
     */
    public static final ThreadLocal<DateFormat> DATE_FORMAT1 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_FORMAT1.getValue()));
    /**
     * yyyy-MM-dd
     */
    public static final ThreadLocal<DateFormat> DATE_FORMAT2 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_FORMAT2.getValue()));
    /**
     * yyyy.MM.dd
     */
    public static final ThreadLocal<DateFormat> DATE_FORMAT3 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_FORMAT3.getValue()));
    /**
     * yyyy年MM月dd日
     */
    public static final ThreadLocal<DateFormat> DATE_FORMAT4 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_FORMAT4.getValue()));
    /**
     * yyyyMMddHHmmss
     */
    public static final ThreadLocal<DateFormat> DATE_TIME_FORMAT1 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_TIME_FORMAT1.getValue()));
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final ThreadLocal<DateFormat> DATE_TIME_FORMAT2 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_TIME_FORMAT2.getValue()));
    /**
     * yyyyMMddHHmmssSSS
     */
    public static final ThreadLocal<DateFormat> DATE_TIME_FORMAT3 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_TIME_FORMAT3.getValue()));
    /**
     * HH:mm:ss
     */
    public static final ThreadLocal<DateFormat> DATE_TIME_FORMAT4 = ThreadLocal.withInitial(() -> new SimpleDateFormat(DateFormatEnum.DATE_TIME_FORMAT4.getValue()));


    /**
     * @descri Date转yyyyMMdd字符串
     * @auther crazy_cat
     * @date 2022/1/14 17:52
     * @param date
     * @return java.lang.String
     * @modify
     */
    public static String dateToStr1(Date date){
        return DATE_FORMAT1.get().format(date);
    }

    /**
     * @describe yyyyMMdd字符串转Date
     * @auther crazy_cat
     * @date 2022/1/14 17:52
     * @param dateStr
     * @return java.util.Date
     * @modify
     */
    public static Date strToDate(String dateStr) throws ParseException {
        try {
            return DATE_FORMAT1.get().parse(dateStr);
        } catch (ParseException e) {
            throw e;
        }
    }

    /**
     * @describe Date转自定义格式字符串
     * @auther crazy_cat
     * @date 2022/1/14 17:59
     * @param date Date对象
     * @param DATE_FORMAT 日期格式对象
     * @return java.lang.String
     */
    public static String dateCustomToStr(Date date, ThreadLocal<DateFormat> DATE_FORMAT){
        return DATE_FORMAT.get().format(date);
    }

    /**
     * @describe 字符串转Date对象
     * @auther crazy_cat
     * @date 2022/1/14 18:00
     * @param dateStr 日期字符串
     * @param DATE_FORMAT
     * @return java.util.Date
     * @modify
     */
    public static Date strCustomToDate(String dateStr, ThreadLocal<DateFormat> DATE_FORMAT) throws ParseException {
        try {
            return DATE_FORMAT.get().parse(dateStr);
        } catch (ParseException e) {
            throw e;
        }
    }
}
